package so.shanku.essential.fragment;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import so.shanku.essential.R;
import so.shanku.essential.activity.ArticalSearchActivity;

import java.util.HashMap;

/**
 * Created by Jaelyn on 2016/3/15.
 * 商城
 */
public class ShopMallFragment extends BaseFragment {

    @ViewInject(R.id.main_top_layout) //title
    private LinearLayout main_top_layout;
    @ViewInject(R.id.title_menu_iv)
    private ImageView title_menu_iv;

    @ViewInject(R.id.item_left_layout) //title
    private LinearLayout item_left_layout;
    @ViewInject(R.id.item_right_layout) //title
    private RelativeLayout item_right_layout;
    @ViewInject(R.id.item_left_tv)
    private TextView item_left_tv;
    @ViewInject(R.id.item_right_tv)
    private TextView item_right_tv;

    @ViewInject(R.id.iv_search)
    private ImageView iv_search;

    /**
     * 保存点击过得fragment
     */
    private HashMap<Integer, BaseFragment> fragments = new HashMap();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.shopmall_fragment_layout, container, false);
        ViewUtils.inject(this, view);
        item_left_tv.setText(R.string.classify);
        item_right_tv.setText(R.string.brand);

        title_menu_iv.setVisibility(View.VISIBLE);
        item_left_tv.setCompoundDrawables(null, null, null, null);
        item_right_tv.setCompoundDrawables(null, null, null, null);
        showLeftFragment();
        return view;
    }

    /*
     * 购物车
     * @param view
     */
    @OnClick(R.id.title_menu_iv)
    public void title_menu_iv_click(View view) {
        activity.jumpToShopCart();
    }


    /**
     * 来源
     *
     * @param view
     */
    @OnClick(R.id.item_left_layout)
    public void item_left_layout_click(View view) {
        showLeftFragment();
    }

    private void showLeftFragment() {
        setSelectLeftRightStatus(1, 0);
        if (fragments.get(0) != null) {
            replaceView(fragments.get(0));
        } else {
            ProductCategoryFragment goodsCategoryFragment = new ProductCategoryFragment();
            replaceView(goodsCategoryFragment);
            fragments.put(0, goodsCategoryFragment);
        }
    }


    /**
     * 排序
     *
     * @param view
     */
    @OnClick(R.id.item_right_layout)
    public void item_right_layout_click(View view) {
        showRightFragment();
    }

    private void showRightFragment() {
        setSelectLeftRightStatus(0, 1);
        if (fragments.get(1) != null) {
            replaceView(fragments.get(1));
        } else {
            BrandAllFragment brandListFragment = new BrandAllFragment();
            replaceView(brandListFragment);
            fragments.put(1, brandListFragment);
        }
    }

    /**
     * 设置左侧按钮状态 0:不选中,1:选中,2:下拉弹框,
     *
     * @param i
     */
    private void setSelectLeftRightStatus(int i, int j) {
       // Drawable popStateDrawable1 = getResources().getDrawable(R.drawable.pop_none);
        Drawable popStateDrawable2 = getResources().getDrawable(R.drawable.pop_down_grey);
        Drawable popStateDrawable3 = getResources().getDrawable(R.drawable.pop_down_white);

        Drawable bottomStateDrawable1 = getResources().getDrawable(R.drawable.hor_line_trans_2);
        Drawable bottomStateDrawable2 = getResources().getDrawable(R.drawable.hor_line_white_2);

        // 调用setCompoundDrawables时，必须调用Drawable.setBounds()方法,否则图片不显示
        popStateDrawable2.setBounds(0, 0, popStateDrawable2.getMinimumWidth(), popStateDrawable2.getMinimumHeight());
        popStateDrawable3.setBounds(0, 0, popStateDrawable3.getMinimumWidth(), popStateDrawable3.getMinimumHeight());

        bottomStateDrawable1.setBounds(0, 0, bottomStateDrawable1.getMinimumWidth(), bottomStateDrawable1.getMinimumHeight());
        bottomStateDrawable2.setBounds(0, 0, bottomStateDrawable2.getMinimumWidth(), bottomStateDrawable2.getMinimumHeight());

        switch (i) {
            case 0:
                item_left_tv.setCompoundDrawables(null, null, null, null);
                item_left_layout.setBackgroundResource(R.drawable.nav_bar);
                break;
            case 1:
                item_left_layout.setBackgroundResource(R.drawable.nav_bar);
                item_left_tv.setCompoundDrawables(null, null, null, bottomStateDrawable2);
                break;
            case 2:
                item_left_layout.setBackgroundResource(R.drawable.nav_bar_selected);
                item_left_tv.setCompoundDrawables(null, null, popStateDrawable3, bottomStateDrawable1);
                break;
            default:
        }
        switch (j) {
            case 0:
                item_right_layout.setBackgroundResource(R.drawable.nav_bar);
                item_right_tv.setCompoundDrawables(null, null, null, null);
                break;
            case 1:
                item_right_layout.setBackgroundResource(R.drawable.nav_bar);
                item_right_tv.setCompoundDrawables(null, null, null, bottomStateDrawable2);
                break;
            case 2:
                item_right_layout.setBackgroundResource(R.drawable.nav_bar_selected);
                item_right_tv.setCompoundDrawables(null, null, popStateDrawable3, bottomStateDrawable1);
                break;
            default:
        }

        if (i == 2 || j == 2) {
            item_left_layout.setClickable(false);
            item_right_layout.setClickable(false);
        } else {
            item_left_layout.setClickable(true);
            item_right_layout.setClickable(true);
        }
    }


    private Fragment currentFragment;

    private void replaceView(BaseFragment fragment) {
        if (currentFragment != fragment) {
            FragmentTransaction transaction = this.getFragmentManager().beginTransaction();
//			FragmentTransaction transaction = mFragmentMan.beginTransaction().setCustomAnimations(
//					android.R.anim.fade_in, R.anim.slide_out);
            if (currentFragment != null) {
                if (!fragment.isAdded()) {    // 先判断是否被add过
                    transaction.hide(currentFragment).add(R.id.shopmall_fragment_content_layout, fragment).commit(); // 隐藏当前的fragment，add下一个到Activity中
                } else {
                    transaction.hide(currentFragment).show(fragment).commit(); // 隐藏当前的fragment，显示下一个
                }
            } else {
                transaction.add(R.id.shopmall_fragment_content_layout, fragment).commit(); // add下一个到Activity中
            }
            currentFragment = fragment;
        }
    }


    /**
     * 搜索图标
     *
     * @param view
     */
    @OnClick(R.id.iv_search)
    public void iv_search_click(View view) {
        Intent findSearchIntent = new Intent(activity, ArticalSearchActivity.class);
        activity.jumpTo(findSearchIntent, false);
    }


}


